package com.winit.openapi.es.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;

public class ElasticUtils {

    private static String buildDateYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date.getTime());

        // 保证获取的年月日时分秒为UTC时区
        TimeZone tztz = TimeZone.getTimeZone("GMT");
        cal.setTimeZone(tztz);

        int year = cal.get(Calendar.YEAR);

        return String.valueOf(year);
    }

    private static String buildDateMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date.getTime());

        // 保证获取的年月日时分秒为UTC时区
        TimeZone tztz = TimeZone.getTimeZone("GMT");
        cal.setTimeZone(tztz);

        int month = cal.get(Calendar.MONTH) + 1;

        return fillString(String.valueOf(month), 2, "0");
    }

    public static String[] buildIndex(String indexPrefix, Date startDate, Date endDate) {

        int startYear = Integer.parseInt(buildDateYear(startDate));
        int startMonth = Integer.parseInt(buildDateMonth(startDate));

        int endYear = Integer.parseInt(buildDateYear(endDate));
        int endMonth = Integer.parseInt(buildDateMonth(endDate));

        List<String> suffixs = new ArrayList<String>();
        for (int i = startYear; i <= endYear; i++) {
            if (i == startYear) {
                int maxMonth = endYear > startYear ? 12 : endMonth;
                buildIndexForYear(suffixs, indexPrefix, startYear, startMonth, maxMonth);
            } else if (i == endYear) {
                buildIndexForYear(suffixs, indexPrefix, endYear, 1, endMonth);
            } else {
                buildIndexForYear(suffixs, indexPrefix, i, 1, 12);
            }
        }
        return suffixs.toArray(new String[suffixs.size()]);
    }

    private static void buildIndexForYear(List<String> suffixs, String indexPrefix, int year, int startMonth,
                                          int endMonth) {
        for (int i = startMonth; i <= endMonth; i++) {
            suffixs.add(indexPrefix + String.valueOf(year) + fillString(String.valueOf(i), 2, "0"));
        }
    }

    public static String buildIndexType(String appname, String version) {
        if (StringUtils.isBlank(appname)) {
            return null;
        }

        if (StringUtils.isNotEmpty(appname) && StringUtils.isNotEmpty(version)) {
            return appname + "_" + version;
        } else {
            return appname;
        }
    }

    public static String fillString(String str, int length, String fillChar) {
        if (!StringUtils.isBlank(str) && str.length() < length) {

            if (StringUtils.isBlank(fillChar)) {
                fillChar = "0";
            }

            int len = length - str.length();
            for (int i = 0; i < len; i++) {
                str = fillChar + str;
            }
        }
        return str;
    }
}
